package org.iwidget.desktop.model;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.w3c.dom.*;

/**
 *
 * @author Muhammad Hakim A
 */


public class ElementPreferences extends Element
{

    public ElementPreferences()
    {
        name = "";
        select = "single";
        type = "";
        style = "";
        kind = "";
        directory = "";
        file = "";
        description = "";
        lines = 3;
        defaultValue = "";
        value = null;
        validator = null;
        validatorMessage = "Invalid form data";
        visible = true;
        notSaved = false;
        minLength = 0;
        maxLength = 255;
        ticks = 10;
        options = new ArrayList();
        tickLabels = new ArrayList();
        extentions = new ArrayList();
        tabs = new ArrayList();
    }

    public ElementPreferences(WidgetElement widget)
    {
        super(widget);
        name = "";
        select = "single";
        type = "";
        style = "";
        kind = "";
        directory = "";
        file = "";
        description = "";
        lines = 3;
        defaultValue = "";
        value = null;
        validator = null;
        validatorMessage = "Invalid form data";
        visible = true;
        notSaved = false;
        minLength = 0;
        maxLength = 255;
        ticks = 10;
        options = new ArrayList();
        tickLabels = new ArrayList();
        extentions = new ArrayList();
        tabs = new ArrayList();
    }

    public void addOption(String option)
    {
        options.add(option);
    }

    public ArrayList getOptionsArray()
    {
        return options;
    }

    public String getOptions()
    {
        StringBuffer sb = new StringBuffer();
        for(int iCtr = 0; iCtr < options.size(); iCtr++)
        {
            if(sb.length() > 0)
                sb.append(',');
            sb.append((String)options.get(iCtr));
        }

        return sb.toString();
    }

    public void setOptions(String s)
    {
        String item;
        for(StringTokenizer tokens = new StringTokenizer(s, ","); tokens.hasMoreTokens(); options.add(item))
            item = (String)tokens.nextElement();

    }

    public void addTickLabel(String option)
    {
        tickLabels.add(option);
    }

    public ArrayList getTickLabels()
    {
        return tickLabels;
    }

    public void addTab(String option)
    {
        tabs.add(option);
    }

    public ArrayList getTabsArray()
    {
        return tabs;
    }

    public void setTabs(String s)
    {
        String item;
        for(StringTokenizer tokens = new StringTokenizer(s, ","); tokens.hasMoreTokens(); tabs.add(item))
            item = (String)tokens.nextElement();

    }

    public void addExtension(String option)
    {
        extentions.add(option);
    }

    public ArrayList getExtensions()
    {
        return extentions;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        name = s;
    }

    public String getSelect()
    {
        return select;
    }

    public void setSelect(String s)
    {
        select = s;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String s)
    {
        type = s;
    }

    public String getStyle()
    {
        return style;
    }

    public void setStyle(String s)
    {
        style = s;
    }

    public String getKind()
    {
        return kind;
    }

    public void setKind(String s)
    {
        kind = s;
    }

    public String getDirectory()
    {
        return directory;
    }

    public void setDirectory(String s)
    {
        directory = s;
    }

    public String getFile()
    {
        return file;
    }

    public void setFile(String s)
    {
        file = s;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String s)
    {
        description = s;
    }

    public void setLines(int i)
    {
        lines = i;
    }

    public int getLines()
    {
        return lines;
    }

    public String getTabName()
    {
        return tabName;
    }

    public void setTabName(String s)
    {
        tabName = s;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String s)
    {
        title = s;
    }

    public String getDefaultValue()
    {
        return defaultValue;
    }

    public void setDefaultValue(String s)
    {
        defaultValue = s;
    }

    public String getValue()
    {
        if(value != null)
            return value;
        else
            return defaultValue;
    }

    public void setValue(String s)
    {
        value = s;
    }

    public String getValidator()
    {
        return validator;
    }

    public void setValidator(String s)
    {
        validator = s;
    }

    public String getValidatorMessage()
    {
        return validatorMessage;
    }

    public void setValidatorMessage(String s)
    {
        validatorMessage = s;
    }

    public void setVisible(boolean b)
    {
        visible = b;
    }

    public boolean getVisible()
    {
        return visible;
    }

    public void setNotSaved(boolean b)
    {
        notSaved = b;
    }

    public boolean getNotSaved()
    {
        return notSaved;
    }

    public void setMinLength(int i)
    {
        minLength = i;
    }

    public int getMinLength()
    {
        return minLength;
    }

    public void setMaxLength(int i)
    {
        maxLength = i;
    }

    public int getMaxLength()
    {
        return maxLength;
    }

    public void setTicks(int i)
    {
        ticks = i;
    }

    public int getTicks()
    {
        return ticks;
    }

    public void parse(Node node)
        throws Exception
    {
        NamedNodeMap map = node.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue());
        }

        NodeList nodes = node.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue());
        }

    }

    private void parseHelper(String nodeName, String value)
    {
        if(nodeName.equals("name"))
            setName(value);
        else
        if(nodeName.equals("title"))
            setTitle(value);
        else
        if(nodeName.equals("description"))
            setDescription(value);
        else
        if(nodeName.equals("defaultValue"))
            setDefaultValue(value);
        else
        if(nodeName.equals("maxLength"))
            setMaxLength(Integer.parseInt(value));
        else
        if(nodeName.equals("minLength"))
            setMinLength(Integer.parseInt(value));
        else
        if(nodeName.equals("maxValue"))
            setMaxLength(Integer.parseInt(value));
        else
        if(nodeName.equals("minValue"))
            setMinLength(Integer.parseInt(value));
        else
        if(nodeName.equals("validator"))
            setValidator(value);
        else
        if(nodeName.equals("validatorMessage"))
            setValidatorMessage(value);
        else
        if(nodeName.equals("lines"))
            setLines(Integer.parseInt(value));
        else
        if(nodeName.equals("option"))
            addOption(value);
        else
        if(nodeName.equals("tab"))
            addTab(value);
        else
        if(nodeName.equals("tabName"))
            setTabName(value);
        else
        if(nodeName.equals("type"))
            setType(value);
        else
        if(nodeName.equals("tickLabel"))
            addTickLabel(value);
        else
        if(nodeName.equals("ticks"))
            setTicks(Integer.parseInt(value));
        else
        if(nodeName.equals("style"))
            setStyle(value);
        else
        if(nodeName.equals("select"))
            setSelect(value);
        else
        if(nodeName.equals("kind"))
            setKind(value);
        else
        if(nodeName.equals("directory"))
            setDirectory(value);
        else
        if(nodeName.equals("extension"))
            addExtension(value);
        else
        if(nodeName.equals("file"))
            setFile(value);
        else
        if(nodeName.equals("notSaved"))
        {
            if(value.equals("true"))
                setNotSaved(true);
            else
                setNotSaved(false);
        } else
        if(nodeName.equals("visible"))
            if(value.equals("true"))
                setVisible(true);
            else
                setVisible(false);
    }

    public ArrayList options;
    public ArrayList tickLabels;
    public ArrayList extentions;
    public ArrayList tabs;
    public WidgetElement widget;
    protected String name;
    protected String select;
    protected String type;
    protected String style;
    protected String kind;
    protected String directory;
    protected String file;
    protected String description;
    protected int lines;
    protected String tabName;
    protected String title;
    protected String defaultValue;
    protected String value;
    protected String validator;
    protected String validatorMessage;
    protected boolean visible;
    protected boolean notSaved;
    protected int minLength;
    protected int maxLength;
    protected int ticks;
}